package com.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.entity.Teacher;
import com.demo.service.TeacherService;
import com.demo.common.vo.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * controller
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    /**
     * 新增
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Teacher teacher){
        teacherService.save(teacher);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id){
        teacherService.removeById(id);
        return Result.success();
    }

    /**
     * 更新
     * @param teacher
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Teacher teacher){
        if(teacher.getId()==null){
            return Result.error("更新记录主键不能为空");
        }
        teacherService.updateById(teacher);
        return Result.success();
    }

    /**
     * 列表
     * @param teacher
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestBody Teacher teacher){
        return Result.success(teacherService.list(new QueryWrapper<>(teacher)));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result list(@RequestParam Integer id){
        return Result.success(teacherService.getById(id));
    }

}
